package br.ufal.tci.nexos.arcolive.tests;

import java.io.Serializable;

import javax.media.protocol.DataSource;

import br.ufal.tci.nexos.arcolive.beans.NetworkPlayer;
import br.ufal.tci.nexos.arcolive.beans.NetworkPlayerEvent;
import br.ufal.tci.nexos.arcolive.beans.NetworkPlayerListener;
import br.ufal.tci.nexos.arcolive.beans.ArCoLIVEPlayer;
import br.ufal.tci.nexos.arcolive.beans.PlayerInternalFrame;
import br.ufal.tci.nexos.arcolive.beans.CannotHandleStreamException;
import br.ufal.tci.nexos.arcolive.beans.CannotPlayStreamException;

public class ArCoLIVENetworkPlayerInternalFrame extends
		PlayerInternalFrame implements NetworkPlayerListener,
		Serializable {

	private NetworkPlayer arcoliveNetworkPlayer;

	private ArCoLIVEPlayer arcolivePlayer;

	public ArCoLIVENetworkPlayerInternalFrame() {

	}

	public void handleStream() {
		arcoliveNetworkPlayer = new NetworkPlayer();
		arcoliveNetworkPlayer.addArcoliveNetworkPlayerListener(this);
		arcoliveNetworkPlayer.setReceptionPort(40000);
		try {
			arcoliveNetworkPlayer.handleStream();
		} catch (CannotHandleStreamException e) {
			e.printStackTrace();
		}
	}

	public void dataSourceReadyEvent(
			NetworkPlayerEvent arCoLIVENetworkPlayerEvent) {
		DataSource dataSource = ((NetworkPlayer) arCoLIVENetworkPlayerEvent
				.getSource()).getReceiveDataSource();
		arcolivePlayer = super.getArcolivePlayer();
		arcolivePlayer.setDataSource(dataSource);
		try {
			arcolivePlayer.play();
		} catch (CannotPlayStreamException e) {
			e.printStackTrace();
		}
	}

}